<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var b = 44;
        let a = 2;
        
        {
            const blocka = 100;
        }
        const request = window.indexedDB.open('blogs',2);
        request.onerror = function(event){
            console.log('数据库打开报错');

        }
        request.onsuccess = function(event){
            db = request.result;

            create();

            // console.log('数据库打开成功，数据：',db);

            // let objectStore = db.creatObjectStore('person',{keyPath:'id'});
        }

        request.onupgradeneeded = function(){
            db  = request.result;
            let objectStore;
            if(!db.objectStoreNames.contains('person')){
                objectStore = db.createObjectStore('person',{kyePath:'id'});
                objectStore.createIndex('name','name',{unique:false}); // 建立一个索引
                objectStore.createIndex('email','email',{unique:true}); // 建立一个索引
            }
        }

        var create = function(){
            // 事务
            let request = db.transaction(['person'],'readwrite')
                .objectStore('person')
                .add({id:1,name:'liuyifei',email:'xt@tmall.com'});

            request.onsuccess = function(){
                console.log('数据写入成功');
            }
            request.onerror = function(){
                console.log('数据写入失败');
            }
        }

        
        
    </script>
</body>
</html>